<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Product as ProductModel;
use app\common\model\Store as storeModel;

/**
 * Class Product 项目接口 合作商家
 * @package app\api\controller
 */
class Product extends Api
{
    /**
     * 获取项目列表
     */
    public function get_list()
    {
        $storeModel = model('Store');
        $list = [];
        try {
            $cate_id = $this->request->param('cate_id');
            $is_classic = $this->request->param('is_classic',0);
            $where = ['store.is_show' => storeModel::IS_SHOW_YES,'p.is_show' => ProductModel::IS_SHOW_YES];
            if($cate_id){
                $where['pc.cate_id'] = $cate_id;
            }
            if($is_classic){
                $where['p.is_classic'] = $is_classic;
            }
            $list = $storeModel->alias('store')
                ->join('web_product_store ps','store.id = ps.store_id','left')
                ->join('web_product p','ps.pro_id = p.id','left')
                ->join('web_product_cate pc','p.id = pc.pro_id','left')
                ->where($where)
                ->order('p.weigh desc')
                ->field('p.*,store.name as store_name,store.image as store_logo,store.en_name as en_store_name')
                ->group('p.id')
                ->paginate();
            foreach ($list as &$value) {
                $value['image'] = getRealImg($value['image']);
                $value['store_logo'] = getRealImg($value['store_logo']);
//                $value['store_name'] = $value['store_name'] ? $value['store_name'] : "自研项目";
            }
        } catch (\Exception $e) {
            new \Exception($e->getMessage(), 0);
        }
        $this->success('ok', $list);
    }

    /**
     * 获取项目详情
     */
    public function get_info()
    {
        $Product_id = $this->request->param('id');
        $ProductModel = model('Product');
        $result = [];
        try {
            $result = $ProductModel->alias('p')
                ->join('web_product_store ps','p.id = ps.pro_id','left')
                ->join('web_cooperation_store store','ps.store_id = store.id','left')
                ->where('p.id', $Product_id)
                ->field("p.*,store.name as store_name,store.image as store_logo,store.en_name as en_store_name")
                ->find();
            if(!$result) {
                $this->error('暂无数据', []);
            }
            $result['browse'] += 1;
            $result['image'] = getRealImg($result['image']);
            $result['info_image'] = getRealImg($result['info_image']);
            $result['store_logo'] = getRealImg($result['store_logo']);
            $info_image = $result['info_image'] ? explode(",",$result['info_image']) : [];
            foreach($info_image as &$item){
                $item = getRealImg($item);
            }
            $result['info_image'] = $info_image;
//            $result['content'] = replace_editor_content_images_url($result['content']);
            model('Product')->where(['id'=>$Product_id])->setInc('browse');
        } catch (\Exception $e) {
            new \Exception($e->getMessage(), 0);
        }
        $this->success('ok', $result);
    }

}